
/**
 * First we will load all of this project's JavaScript dependencies which
 * includes Vue and other libraries. It is a great starting point when
 * building robust, powerful web applications using Vue and Laravel.
 */

require('./bootstrap');

import Vue from 'vue';
import Router from 'vue-router';
import App from './components/App.vue';
import routes from './router';

Vue.use(Router)
let router = new Router({
    routes: []
});

// 路由全局钩子
router.beforeEach((to, from, next) => {
    // 面包屑
    let name = to.name;
    if (name != null){
        Laravel.breadcrumb = [];
        if (name === 'home'){
            Laravel.headerTitle = '首页';
        }else{
            if (name.indexOf('.') == -1){
                name += '.index';
            }
            name = 'admin.' + name;
            name = Laravel.permissions[name];
            Laravel.headerTitle = name.title;

            while(name.pid){
                $.each(Laravel.permissions, (key, value) => {
                    if (value.id == name.pid){
                        name = value;
                        Laravel.breadcrumb.unshift(value.title);
                        return false;
                    }
                });
            }
        }
    }

    next();
})

// 获取用户、权限信息
window.Laravel = {
    submitted: false,
    headerTitle: '',
    breadcrumb: [],
};

$.ajax({
    url: 'index',
    async: false,
    type: 'POST',
}).then(response => {
    let navigation = [];
    let permissions = {};
    let router = [
        {
            path: '/',
            name: 'home',
            component: require('./components/ExampleComponent.vue')
        }
    ];

    Laravel.user = response.user;
    $.each(response.permissions, (key, value) => {
        permissions[value.name] = value;

        // 导航菜单
        if (value.type){
            let url = value.name.split('.');
            if (url[0] === 'admin'){
                url.splice(0, 1);
            }
            if (url[1] === 'index'){
                url.splice(1, 1);
            }
            value.url = url.join('/');
            navigation.push(value);
        }
    });

    // Vue路由
    $.each(routes, (key, value) => {
        let name = value.name;
        if (name.indexOf('.') == -1){
            name += '.index';
        }
        if (permissions.hasOwnProperty('admin.' + name)){
            router.push(value);
        }
    });

    Laravel.router = router;
    Laravel.navigation = navigation;
    Laravel.permissions = permissions;
});

/**
 * validator
 */
import zh_CN from './locale/zh_CN';
import VeeValidate, {Validator} from 'vee-validate';

Validator.localize('zh_CN', zh_CN);

Vue.use(VeeValidate, {
    locale: 'zh_CN'
});

/**
 * Next, we will create a fresh Vue application instance and attach it to
 * the page. Then, you may begin adding components to this application
 * or customize the JavaScript scaffolding to fit your unique needs.
 */

Vue.component('example-component', require('./components/ExampleComponent.vue'));
Vue.component('btn-add', require('./components/layouts/btn-add.vue'));
Vue.component('btn-addp', require('./components/layouts/btn-addp.vue'));
Vue.component('btn-edit', require('./components/layouts/btn-edit.vue'));
Vue.component('btn-destroy', require('./components/layouts/btn-destroy.vue'));
Vue.component('pagination', require('./components/layouts/pagination.vue'));

// 导航菜单
Vue.component('nav-menu', require('./components/layouts/nav-menu.vue'));

// 分配角色
Vue.component('give-role', require('./components/layouts/give-role/give-role.vue'));
Vue.component('btn-give-role', require('./components/layouts/give-role/btn-give-role.vue'));

// 分配权限
Vue.component('btn-give-permission', require('./components/layouts/give-permission/btn-give-permission.vue'));
Vue.component('give-permission', require('./components/layouts/give-permission/give-permission.vue'));
Vue.component('give-permission-tree', require('./components/layouts/give-permission/give-permission-tree.vue'));

const app = new Vue({
    router,
    render: h => h(App),
    data: Laravel,
    components: { App },
}).$mount('#app');
